/**
 * 
 */
package fhq.hcmute.edu.vn.security;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

/**
 * @author nglehien Nov 17, 2012 - 1:57:28 PM
 */
public class KeyManagement {

	public static byte[] encrypt(byte[] inpBytes, PublicKey key)
			throws Exception {
		Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		return cipher.doFinal(inpBytes);
	}

	public static byte[] decrypt(byte[] inpBytes, PrivateKey key)
			throws Exception {
		Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		cipher.init(Cipher.DECRYPT_MODE, key);
		return cipher.doFinal(inpBytes);
	}

	public static void saveKey(ObjectOutputStream oout, Object key) {
		try {
			oout.writeObject(key);
			oout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
